/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "hmac.h"
#include <cassert>
#include <cstring>
#include "bignumber.h"

namespace node {
    namespace crypto {

        HmacHash::HmacHash()
        {
            ::std::uint8_t temp[SEED_KEY_SIZE] = { 0x38, 0xA7, 0x83, 0x15, 0xF8, 0x92, 
                0x25, 0x30, 0x71, 0x98, 0x67, 0xB1, 0x8C, 0x4, 0xE2, 0xAA };
            memcpy(&key_, &temp, SEED_KEY_SIZE);
            HMAC_CTX_init(&ctx_);
            HMAC_Init_ex(&ctx_, &key_, SEED_KEY_SIZE, EVP_sha1(), NULL);
        }

        HmacHash::~HmacHash()
        {
            memset(&key_, 0x00, SEED_KEY_SIZE);
            HMAC_CTX_cleanup(&ctx_);
        }

        void HmacHash::UpdateBigNumber(BigNumber* bn)
        {
            UpdateData(bn->AsByteArray(), bn->GetNumBytes());
        }

        void HmacHash::UpdateData(const ::std::uint8_t* data, int size)
        {
            HMAC_Update(&ctx_, data, size);
        }

        void HmacHash::Initialize()
        {
            HMAC_Init_ex(&ctx_, &key_, SEED_KEY_SIZE, EVP_sha1(), NULL);
        }

        void HmacHash::Finalize()
        {
            ::std::uint32_t size = 0;
            HMAC_Final(&ctx_, digest_, &size);
            assert(size == SHA_DIGEST_LENGTH);
        }

    }
}